﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HenrryTools.Common.Computer
{
    /// <summary>
    /// 获取电脑详细信息
    /// </summary>
    public class ComputerHepl
    {
        /// <summary>
        /// 通过sb获取计算机信息
        /// </summary>
        /// <returns></returns>
        public static string Compute()
        {
            Computer host = Common.Computer.Computer.CreateComputer();
            StringBuilder sb = new StringBuilder();
            #region 获取CPU信息
            Tuple<string, string> t = host.GetCPU();
            sb.AppendLine("*******************    【CPU信息】    *******************");

            CPUInfo procc = host.GetCPUInfo();

            sb.AppendLine("CPU名称：" + procc.Name);
            sb.AppendLine("CPU主频：" + procc.MaxClockSpeed + "MHz");
            sb.AppendLine("CPU核心数：" + procc.NumberOfCores);
            sb.AppendLine("CPU启用核心数：" + procc.NumberOfEnabledCore);
            sb.AppendLine("CPU逻辑处理器数量：" + procc.NumberOfLogicalProcessors);
            sb.AppendLine("二级缓存大小：" + procc.L2CacheSize + "KB");
            sb.AppendLine("三级缓存大小：" + procc.L3CacheSize + "KB");

            sb.AppendLine("描述：" + procc.Description);
            sb.AppendLine("位数处理：" + procc.DataWidth);
            sb.AppendLine("依赖于架构处理器的版本号：" + procc.Version);

            sb.AppendLine("制造商：" + procc.Manufacturer);
            sb.AppendLine("序列号：" + procc.SerialNumber);


            sb.AppendLine("固件是否可以虚拟化扩展：" + (procc.VirtualizationFirmwareEnabled.ToUpper() == "TRUE" ? "是" : "否"));
            sb.AppendLine("处理器是否支持Intel或AMD虚拟机监控器扩展：" + (procc.VMMonitorModeExtensions.ToUpper() == "TRUE" ? "是" : "否"));
            sb.AppendLine("状态：" + procc.Status);
            //sb.AppendLine("CPU名称：" + t.Item1);
            //sb.AppendLine("CPU主频：" + t.Item2);
            //sb.AppendLine("CPU核心数：" + host.GetCPU_Count());
            sb.AppendLine();
            #endregion
            #region 获取内存信息
            sb.AppendLine("*******************    【内存信息】    *******************");
            MemoryView mv = host.GetPhisicalMemoryInfo();
            sb.AppendLine("总内存大小：" + mv.TotalSize);
            sb.AppendLine("内存条数量：" + mv.MemoryCount);
            for (int i = 0; i < mv.MemoryCardList.Count; i++)
            {
                sb.AppendLine("【内存条" + (i + 1) + "】");
                sb.AppendLine("制造商:" + mv.MemoryCardList[i].Manufacturer + ";内存容量：" + CommonUtlity.ToGB(double.Parse(mv.MemoryCardList[i].Capacity), 1024) + ";");
                sb.AppendLine("电压：" + mv.MemoryCardList[i].Voltage + "mV;时钟频率：" + mv.MemoryCardList[i].ConfiguredClockSpeed + "MHz;数据宽度：" + mv.MemoryCardList[i].DataWidth + "bits;");
                sb.AppendLine("出厂编号：" + mv.MemoryCardList[i].PartNumber.Trim() + ";序列号：" + mv.MemoryCardList[i].SerialNumber + ";");

            }
            sb.AppendLine();
            #endregion
            #region 获取磁盘信息
            sb.AppendLine("*******************    【磁盘信息】    *******************");
            DiskView dv = host.GetDiskInfo();

            sb.AppendLine("磁盘容量：" + CommonUtlity.ToGB(dv.PhisicalDiskInfo.Size, 1000));
            sb.AppendLine("型号：" + dv.PhisicalDiskInfo.Model);
            sb.AppendLine("序列号：" + dv.PhisicalDiskInfo.SerialNumber);
            sb.AppendLine("接口类型：" + dv.PhisicalDiskInfo.InterfaceType);
            sb.AppendLine("制造商：" + dv.PhisicalDiskInfo.Manufacturer);
            sb.AppendLine();
            //******************* 逻辑磁盘 *******************//
            List<LogicalDisk> logicList = dv.LogicalDiskListInfo;
            if (logicList.Count() > 0)
            {
                int fixLength = 20;

                for (int i = 0; i < logicList.Count(); i = i + 2)
                {
                    if (logicList.Count == 1 || logicList.Count() % 2 == 1 && (logicList.Count() == i + 1))
                    {
                        sb.AppendLine("盘符：" + logicList[i].DeviceID);
                        sb.AppendLine("卷标：" + logicList[i].VolumeName);
                        sb.AppendLine(string.Format("总空间:{0}", CommonUtlity.ToGB(double.Parse(logicList[i].Size), 1000)));
                        sb.AppendLine(string.Format("占用:{0}", CommonUtlity.ToGB(double.Parse(logicList[i].UseSpace), 1000)));
                        sb.AppendLine(string.Format("可用空间:{0}", CommonUtlity.ToGB(logicList[i].FreeSpace, 1000)));

                        sb.AppendLine("文件系统类型：" + logicList[i].FileSystem);
                    }//个数为1 或者 （个数为奇数且为最后一个元素）
                    else
                    {
                        sb.AppendLine(("盘符：" + logicList[i].DeviceID).PadRight(fixLength) + "盘符：" + logicList[i + 1].DeviceID);
                        sb.AppendLine(("卷标：" + logicList[i].VolumeName).PadRight(fixLength) + "卷标：" + logicList[i + 1].VolumeName);
                        sb.AppendLine(("总空间：" + CommonUtlity.ToGB(double.Parse(logicList[i].Size), 1000)).PadRight(fixLength) + "总空间：" + CommonUtlity.ToGB(double.Parse(logicList[i + 1].Size), 1000));
                        sb.AppendLine(("占用：" + CommonUtlity.ToGB(double.Parse(logicList[i].UseSpace), 1000)).PadRight(fixLength) + "占用：" + CommonUtlity.ToGB(double.Parse(logicList[i + 1].UseSpace), 1000));
                        sb.AppendLine(("可用空间：" + CommonUtlity.ToGB(logicList[i].FreeSpace, 1000)).PadRight(fixLength) + "可用空间：" + CommonUtlity.ToGB(logicList[i + 1].FreeSpace, 1000));

                    }

                    sb.AppendLine();
                }
            }
            else
            {
                sb.AppendLine("无逻辑磁盘");
            }

            sb.AppendLine();
            #endregion
            #region 显卡信息
            sb.AppendLine("*******************    【显卡信息】    *******************");
            GraphicsView gv = host.GetGraphicsInfo();

            foreach (var item in gv.CardList)
            {
                sb.AppendLine("名称：" + item.Description);
                sb.AppendLine("显存大小：" + CommonUtlity.ToGB(double.Parse(item.AdapterRAM.ToString()), 1024));
                sb.AppendLine("当前分辨率、颜色：" + item.VideoModeDescription);
                sb.AppendLine("最大刷新频率：" + (item.MaxRefreshRate == 0 ? "" : item.MaxRefreshRate.ToString()));
                sb.AppendLine("最小刷新频率：" + (item.MinRefreshRate == 0 ? "" : item.MinRefreshRate.ToString()));

                sb.AppendLine("比特数显示每个像素：" + (item.CurrentBitsPerPixel == 0 ? "" : item.CurrentBitsPerPixel.ToString()));

                sb.AppendLine("模拟转换器（DAC）芯片的标识符：" + item.AdapterDACType);
                sb.AppendLine("驱动程序最后修改时间：" + item.DriverDate);
                sb.AppendLine("显卡处理器：" + item.VideoProcessor);
                sb.AppendLine("驱动版本：" + item.DriverVersion);
                sb.AppendLine("状态：" + item.Status);

                sb.AppendLine();
            }

            sb.AppendLine();
            #endregion

            #region 获取主板信息
            sb.AppendLine("*******************    【主板信息】    *******************");
            BaseBoard bb = host.GetBaseBoard();
            sb.AppendLine("产品名：" + bb.Product);
            sb.AppendLine("制造商：" + bb.Manufacturer);
            sb.AppendLine("序列号：" + bb.SerialNumber);
            sb.AppendLine("SKU：" + bb.SKU);
            sb.AppendLine("描述：" + bb.Description);
            sb.AppendLine("状态：" + bb.Status);
            sb.AppendLine();
            #endregion
            #region 计算机产品
            sb.AppendLine("*******************   【计算机产品】   *******************");

            ComputerProductInfo cp = host.GetComputerProductInfo();
            sb.AppendLine("名称：" + cp.Name);
            sb.AppendLine("版本：" + cp.Version);
            sb.AppendLine("供应商：" + cp.Vendor);

            sb.AppendLine("SKU：" + cp.SKU);
            sb.AppendLine("产品标识：" + cp.IdentifyingNumber);
            sb.AppendLine("UUID：" + cp.UUID);

            sb.AppendLine("简短描述：" + cp.Caption);
            sb.AppendLine("描述：" + cp.Description);

            sb.AppendLine();

            #endregion
            #region 获取软件环境信息
            sb.AppendLine("*******************    【软件环境】    *******************");
            //sb.AppendLine("用户名：" + host.GetCurrUserName());
            sb.AppendLine("计算机名：" + host.ComputerName());
            sb.AppendLine("用户名：" + host.CurUserName());
            sb.AppendLine("域名：" + host.DominName());

            sb.AppendLine("计算机位数：" + host.OperationSystemBit());
            sb.AppendLine("平台标识符、版本：" + host.OSVersionStr());
            sb.AppendLine("系统目录：" + host.SystemDirectory());
            sb.AppendLine();

            #endregion
            return sb.ToString();
        }
    }
}
